<?php
/*
 * @分销商城
 * @功能说明：商品管理中心
 * @更新说明：暂无更新
 * @文件名 GoodsController.class.php
 * @编码 UTF-8
 * @创建时间 2015-12-25 上午11:50:21
 * @创建人 XING
 */
namespace dg\Controller;
use Think\Controller;
class OmgController extends ApibaseController {
	/*
	 * 分销商城_商品管理
	 * @date: 2015-12-25 上午11:50:41
	 * @editor: XING
	 */
    public function index(){
    	/*参数初始化*/
    	$page = I('get.page') ?: 1;
    	$perpage = 10;$url = '';
    	$list = M('OmgPrice');
    	$url = array();
    	if(I('get.omg_title')){
    		$list = $list->where('omg_title LIKE "%'.I('get.omg_title').'%"');
    		$url['omg_title'] = I('get.omg_title');
    	}
    	$sObject = clone $list;
    	$getData = $list->order(array('omg_sales'=>'desc'))->page($page,$perpage)->select();
    	$this->assign('page',setPage($sObject->count(),$perpage,$page,'?'.http_build_query($url)));
    	$this->assign('getData',$getData);

    	$this->display('omg/index');
    }
    
	/*
	 * 分销商城_添加商品
	 * @date: 2015-12-25 下午12:16:16
	 * @editor: XING
	 */
    public function addGoods(){
    	if(I('post.dosubmit') && I('post.url')){
    		$url = trim(I('post.url'));

    		$data = file_get_contents($url);
    		preg_match('/(\S)*id\=\"js_currentPrice\">¥<span>(\S*)<\/span><\/span>(\S)*/',$data,$matches);
    		if($matches && $matches['2']){
    			$putEffect = M('OmgPrice')
    					   ->where(array('sku_id'=>I('post.sku_id')))->setField(array(
    							'kl_price' => $matches['2'],
    					   		'kl_url' => $url
    						));
    			$this->showtips('网址采集成功');
    		}else{
    			$this->showtips('操作失败');
    		}
    	}
    	if(I('get.sku_id')){
    		$getData = M('GoodsEntity')
    				 ->where(array('sku_id'=>I('get.sku_id')))->find();
    		$this->assign('getData',$getData);
    	}
        $this->assign('sku_id',I('get.sku_id'));
    	$this->display('omg/addgoods');
    }
    
    /*
	 * 分销商城_采集OMG
	 * @date: 2016-4-26 上午11:31:54
	 * @editor: XING
	 */
    public function putCollect(){
    	for($i=1;$i<=77;$i++){
    		$data = file_get_contents('http://wap.100fua.com/Handler/ProductHandler.ashx?action=ShopDefault&math=0.2766594234853983&pageIndex='.$i.'&Keywords=&openId=ooSBXxFlVGQrn1hzd4mg4SzbScSY&pageSize=10');
    		$dataArr = json_decode($data,1);
    		if($dataArr['result'] == 'success' && $dataArr['List']){
    			foreach($dataArr['List'] AS $varr){
    				$checkExist = M('OmgPrice')
    							->where(array('sku_id'=>$varr['Sku']))->find();
    				if($checkExist){
    					M('OmgPrice')
	    					->where(array('sku_id'=>$varr['Sku']))->setField(array(
		    					'omg_title' => $varr['Name'],
		    					'omg_pic' => $varr['ImgUrl'],
		    					'omg_url' => 'wap.100fua.com/p/'.$varr['Sku'].'.html',
	    					));
    				}else{
    					$omgOrder = M('OmgPrice')->add(array(
    							'sku_id' => $varr['Sku'],
    							'omg_title' => $varr['Name'],
    							'omg_pic' => $varr['ImgUrl'],
    							'omg_url' => 'wap.100fua.com/p/'.$varr['Sku'].'.html',
    							'add_at' => time()
    					));
    	
    				}
    				$getTongjiSales = M('OmgTongji')->field('salesAll')
    								->where('from_unixtime(datetime,"%Y-%m-%d")) = "'.date('Y-m-d',time()-24*3600).'" AND sku_id = "'.$varr['Sku'].'"')->find();
    				M('OmgTongji')->add(array(
		    				'sku_id' => $varr['Sku'],
		    				'datetime' => time(),
		    				'price' => $varr['Price'],
		    				'salesAll' => $varr['SellCount'],
		    				'salesToday' => $getTongjiSales && $getTongjiSales['price'] ? $varr['SellCount']-$getTongjiSales['salesAll'] : 0,
		    				'kl_price' => $checkExist['kl_url'] ? '' : '',
		    				'add_at' => time()
	    				));
    			}
    		}
    	}
    	
    	jsonRespons(1, '采集任务已完成');
    }
    
    

    /*
	 * 分销商城_统计折线图显示OMG
	 * @date: 2016-4-29 下午14:33:54
	 * @editor: yanyan
	 */
    public function getCount(){
        if(I('get.sku_id')){
            if(I('get.sel_date') || I('get.sel_date2')){
                $date_start = I('get.sel_date') ? strtotime(I('get.sel_date')) : strtotime(I('get.sel_date2'))-(30*24*60*60);
                $date_end = I('get.sel_date2') ? strtotime(I('get.sel_date2')) : time();
                //echo M('OmgTongji')->getLastSql();exit;
            }else{
                //默认显示最近一个月的
                $date_start = time()-(30*24*60*60);
                $date_end = time();
                //redirect('/omg/getCount/ifm/1?sel_date='.$date_start.'&sel_date2='.$date_end);
                /*$getData = M('OmgTongji')
                    ->where(array('sku_id'=>I('get.sku_id')))->select();*/
            }
            $date_start_str = date('Y-m-d H:i:s',$date_start);
            $date_end_str = date('Y-m-d H:i:s',$date_end);
            $getData = M('OmgTongji')->where('`datetime` between "'.$date_start.'" and "'.$date_end.'" and sku_id='.I('get.sku_id'))->select();

            $getgoods = M('OmgPrice')
                ->where(array('sku_id'=>I('get.sku_id')))->find();
            $goodsname = $getgoods['omg_title'];
            $str_time = array();
            $str_price = array();
            $kl_price = array();
            $str_daysales = array();
            foreach($getData as $k=>$v){
                $getData[$k]['date'] = date('Y/m/d',$v['datetime']);
                $str_time[] = "'".$getData[$k]['date']."'";
                $str_price[] = $v['price'];
                $kl_price[] = $v['kl_price'];
                $str_daysales[] = $v['salestoday'];
            }
            $str_time = implode($str_time,',');
            $str_price = implode($str_price,',');
            $kl_price = implode($kl_price,',');
            $str_daysales = implode($str_daysales,',');
            $this->assign('date_start_str',$date_start_str);
            $this->assign('date_end_str',$date_end_str);
            $this->assign('str_time',$str_time);
            $this->assign('str_price',$str_price);
            $this->assign('kl_price',$kl_price);
            $this->assign('str_daysales',$str_daysales);
            $this->assign('goodsname',$goodsname);
            $this->assign('getData',$getData);
        }
        $this->display('omg/countgoods');
    }
}